/**
* @Title: SimpleChatServer.java
* @Package com.an.simplechat
* @Description: TODO(用一句话描述该文件做什么)
* @author antch,861710865@qq.com
* @date 2017年7月21日 上午9:36:55
* @version V1.0
*/

package com.an.simplechat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * @ClassName: SimpleChatServer
 * @Description: TODO(服务器启动类)
 * @author antch,861710865@qq.com
 * @date 2017年7月21日 上午9:36:55
 *
 */

public class SimpleChatServer {

	private int port;

	public SimpleChatServer(int port) {
		super();
		this.port = port;
	}

	public void run() throws InterruptedException {
		EventLoopGroup bossGroup = new NioEventLoopGroup();
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try {
			ServerBootstrap b = new ServerBootstrap();
			b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
					.childHandler(new SimpleChatServerInitializer()).option(ChannelOption.SO_BACKLOG, 128)
					.childOption(ChannelOption.SO_KEEPALIVE, true);

			System.out.println("****服务器启动开始****");
			// 绑定端口，开始接收进来的连接
			ChannelFuture f = b.bind(port).sync();
			System.out.println("****服务器启动成功****");

			// 等待服务器socket关闭
			// 在这个例子中，不会发很，但你可以优雅的关闭你的服务器
			f.channel().closeFuture().sync();
		} finally {
			workerGroup.shutdownGracefully();
			bossGroup.shutdownGracefully();

			System.out.println("SimpleChatServer 关闭了");
		}
	}

	/**
	 * @Title: main @Description: TODO(服务器启动，默认8080端口) @param @param args
	 * 设定文件 @return void 返回类型 @throws
	 */

	public static void main(String[] args) {

		try {
			new SimpleChatServer(8080).run();
		} catch (InterruptedException e) {
			System.out.println("**启动异常**");
		}

	}

}
